package edu.utn.tacs.grupouno.ui.client;

import java.util.Date;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;

import edu.utn.tacs.grupouno.ui.client.dto.TurnoDTO;

public class TurnoDetailsPanel extends HorizontalPanel {

	private TurnoDTO turno;
	private TurnoActionExecutor executor;

	public TurnoDetailsPanel(TurnoDTO turno, TurnoActionExecutor executor) {
		this.executor = executor;
		this.turno = turno;
		initComponents();
	}
	
	public TurnoDetailsPanel(TurnoDTO turno) {
		this.turno = turno;
		initComponents();
	}

	private void initComponents() {
		setStyleName("detalleCell");
		Panel leftPanel = new VerticalPanel();
		Panel centerPanel= new VerticalPanel();
		Panel rightPanel= new VerticalPanel();
		
		String minutos = turno.getHorario().substring(turno.getHorario().lastIndexOf(":") + 1);
		leftPanel.add(new Label(minutos));
		centerPanel.add(new Label(turno.isDisponible() ? "Disponible" : "Asignado"));
		
		if(!turno.isDisponible()) {
			centerPanel.add(new Label("Medico: " + turno.getAnalisis().getNombreMedico()));
			centerPanel.add(new Label("Paciente: " + turno.getAnalisis().getNombrePaciente()));
			if(DateUtil.fromStrings(DateUtil.toString(turno.getFecha()), turno.getHorario()).before(new Date())) {
				centerPanel.add(new Label(turno.isAsistio() ? "El paciente asistio" : "El paciente se ausento"));
			}
			Button button = new Button("Ver analisis");
			button.addClickHandler(new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent arg0) {
					Panel panel = Application.getContentPanel();
					panel.clear();
					panel.add(new AnalisisDetallePanel(turno.getAnalisis().getId()));
				}
			});
			rightPanel.add(button);
		}
		if(executor != null && executor.canExecute(turno)) {
			rightPanel.add(executor.getWidget(turno));
		}
		add(leftPanel);
		add(centerPanel);
		add(rightPanel);
	}
}
